package com.foreverwin.jzymes.production.tool.service.impl;

import com.foreverwin.jzymes.common.mapper.ToolAdiviceMapper;
import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.service.IToolAdiviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.tool.domain.TOOL;
import com.foreverwin.jzymes.production.tool.mapper.TOOLMapper;
import com.foreverwin.jzymes.production.tool.service.ITOOLAdiviceService;
import com.foreverwin.jzymes.production.tool.service.ITOOLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.tools.Tool;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ITOOLAdiviceServiceImpl implements ITOOLAdiviceService {
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    ITOOLService itoolService;
    @Autowired
    IToolAdiviceService toolAdiviceService;
    @Autowired
    ToolAdiviceMapper toolAdiviceMapper;
    @Autowired
    TOOLMapper toolMapper;
    /**
     * 增加刀具信息
     * @param tool
     *
     */
    @Override
    public void saveToolData(TOOL tool) {
        /**校验信息*/
       checksaveTool( tool );

       String handle = BOHandleUtil.buildToolHandle( tool.getSite() , tool.getToolType() );

       tool.setHandle( handle );
       tool.setCreatedDateTime( new Date());
       tool.setCreateUserBo( CommonMethods.getUserBo() );

       TOOL tool1 = itoolService.selectTOOLById( handle );

       if ( tool1 == null ){
           itoolService.insertTOOL( tool );
       }else {
           /*tool1.setToolDesc( tool.getToolDesc() );
           tool1.setToolSpec( tool.getToolSpec() );
           tool1.setBladeLength( tool.getBladeLength() );
           tool1.setCollet( tool.getCollet() );
           tool1.setUpdateUserBo( tool.getCreateUserBo() );
           tool1.setUpdatedDateTime( tool.getCreatedDateTime() );

           itoolService.updateTOOL( tool1 );*/
           throw BusinessException.build("production.tool.toolTypeIsExist",new String[]{ tool.getToolType() });
       }

    }
    /**
     * 删除刀具信息
     * @param list
     *
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deteleToolData(List<String> list) {
        String site = CommonMethods.getSite();
        /**站点不能为空*/
        siteAdviceService.existSite( site );
        String ids = null ;
         for (int i = 0;i<list.size();i++){
             /**检查刀具*/
             toolAdiviceService.selectToolMap( list.get(i),site );
             /**检查刀具是否引用*/
             List<Map<String,Object>> mapList =  toolAdiviceMapper.findToolBillByToolType( list.get(i) );

            if ( mapList.size() > 0 ){

                String toolbillBo = (String)( mapList.get(0) ).get("TOOL_BILL_BO");
                String[] tool = toolbillBo.split(",");
                throw BusinessException.build("production.tool.toolTypeIsQuote",new String[]{ tool[2],list.get(i) });
            }

            String handle = BOHandleUtil.buildToolHandle( site , list.get(i) );

            toolMapper.deleteTOOLById( handle );
         }

    }

    /**
     * 修改刀具信息
     * @param tool
     *
     */
    @Override
    public void editToolData(TOOL tool) {
       /**检查字段*/
       checksaveTool( tool );

       String handle = BOHandleUtil.buildToolHandle( tool.getSite() , tool.getToolType() );
       TOOL tool1 = itoolService.selectTOOLById( handle );
       Assert.notNull( tool1 , I18nUtil.getI18nText( "production.tool.toolTypeNotExist" , new String[] {tool.getToolType()}) );

        tool1.setToolDesc( tool.getToolDesc() );
        tool1.setToolSpec( tool.getToolSpec() );
        tool1.setBladeLength( tool.getBladeLength() );
        tool1.setCollet( tool.getCollet() );
        tool1.setUpdateUserBo( CommonMethods.getUserBo() );
        tool1.setUpdatedDateTime( new Date() );

        itoolService.updateTOOL( tool1 );
    }

    /**
     * 校验字段
     *
     */
    public void  checksaveTool(TOOL tool){
         /**检查站点*/
         siteAdviceService.existSite( tool.getSite() );
         /**刀具类型*/
        Assert.hasText( tool.getToolType() , I18nUtil.getI18nText( "production.tool.toolType" ) );
        /**刀具名称*/
        Assert.hasText( tool.getToolDesc() , I18nUtil.getI18nText( "production.tool.toolDesc" ) );
        /**刀具规格*/
        Assert.hasText( tool.getToolSpec() , I18nUtil.getI18nText( "production.tool.toolSpec" ) );
        /**有效刃长*/
        Assert.notNull( tool.getBladeLength() , I18nUtil.getI18nText( "production.tool.bladeLength" ) );
        /**筒夹*/
        Assert.hasText( tool.getCollet() , I18nUtil.getI18nText( "production.tool.collet" ) );
    }
}
